<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of userEditModel
 * 用户编辑字段模型
 * @author xlp
 */
class userEditModel extends model {

    function __construct() {
        parent::__construct();
        $this->dbTable = 'user_edit_count';
    }

    public function getEditTip($table, $field) {
        $conf = C('user', 'edits');
        if (!isset($conf[$table][$field])) {
            return array('status' => 0, 'count' => -1, 'msg' => '未设置');
        }
        $check = $conf[$table][$field];
        if ($check['counts'] > 0) {
            return array('status' => 1, 'counts' => $check['counts'], 'desc' => $check['desc']);
        }
        //不限制
        if ($check['counts'] == -1) {
            return array('status' => 1, 'counts' => -1, 'desc' => $check['desc']);
        }
        //如果当前组别不允许发布则立即返回
        if ($check['counts'] == 0 || $check['counts'] < -1) {
            return array('status' => 0, 'count' => -2, 'msg' => $check['title'] . '不允许修改');
        }
    }

    /*
     * 检查用户是否有修改的机会
     * @param $table string 数据表名称
     * @param $field string 字段名
     * @return int -1:不限制 0:不能修改 >0:具体次数
     */

    public function checkEditCounts($uid, $table, $field) {
        $conf = C('user', 'edits');
        if (!isset($conf[$table][$field])) {
            return array('status' => 1, 'count' => -1, 'msg' => '未设置');
        }
        $check = $conf[$table][$field];
        //不限制
        if ($check['counts'] == -1 || in_array($uid, C('user', 'superUser'))) {
            return array('status' => 1, 'count' => -1, 'msg' => '不限制');
        }
        //如果当前组别不允许发布则立即返回
        if ($check['counts'] < -1) {
            return array('status' => 0, 'count' => -2, 'msg' => $check['title'] . '不允许修改');
        }
        $stime = 0;
        $etime = 0;
        switch ($check['type']) {
            case 'year':
                $stime = mktime(0, 0, 0, 1, 1, date('Y', TIME));
                $etime = mktime(0, 0, 0, 12, 31, date('Y', TIME));
                break;
            case 'month':
                $stime = mktime(0, 0, 0, date('m', TIME), 1, date('Y', TIME));
                $etime = mktime(23, 59, 59, date('m', TIME), date('t'), date('Y', TIME));
                break;
            case 'week':
                $stime = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y'));
                $etime = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y'));
                break;
            case 'day':
                $stime = mktime(0, 0, 0, date('m', TIME), date('d', TIME), date('Y', TIME));
                $etime = mktime(23, 59, 59, date('m', TIME), date('d', TIME), date('Y', TIME));
                break;
        }
        $count = $this->where(array(
                    'AND' => array(
                        'uid' => $uid, 'table_name' => $table, 'field_name' => $field, 'infotime[>=]' => $stime, 'infotime[<=]' => $etime
            )))->count();
        $has = $check['counts'] - $count;
        //返回剩余的次数
        if ($has > 0) {
            return array('status' => 1, 'count' => $has, 'msg' => '');
        } else {
            return array('status' => 0, 'count' => 0, 'msg' => $check['title'] . '修改次数已达上限');
        }
    }

    /*
     * 增加发布的记录信息
     * @param $uid int UID
     * @param $table string 表名
     * @param $field string 字段名
     * @return int
     */

    public function addUserEditInfo($uid, $table, $field) {
        $conf = C('user', 'edits');
        if (!isset($conf[$table][$field])) {
            return -1;
        }
        if (!$uid) {
            return 0;
        }
        return $this->insert(array(
                    'uid' => $uid, 'table_name' => $table, 'field_name' => $field, 'infotime' => TIME
        ));
    }

}
